<?php
namespace app\common\controller;

use app\common\model\SmsModel;

class Sms
{

    public function send()
    {
    }

    /**
     * 验证手机验证码
     * @param string $mobile 手机号
     * @param string $code 验证码
     * @param string $event 事件
     * @param int $expire 过期时间
     * @param int $failTime 容忍的次数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function check(string $mobile , string $code , string $event , int $expire = 90 , int $failTime = 10)
    {
        $model = new SmsModel();
        $row = $model->where([
            'mobile' => $mobile,
            'event' => $event
        ])->order('id' , 'desc')->find();
        if(empty($row)){
            return [ false , '请先发送验证码'];
        }elseif($row->created_at < time() - $expire){
            return [ false , '验证码已过期'];
        }elseif ($row->code !== $code){
            $row->inc('times' , 1)->update();
//            $row->save([
//                'times' => $row->times + 1
//            ]);
            return [ false , '验证码错误'];
        }
        return [ true , '验证通过'];
    }






}